<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <link rel="stylesheet" href="../css/wzl-bootstrap.css" />
    <title>余额充积分</title>
    <style>

    </style>
</head>
<body>
    <div>
        <nav class="wzl-navbar navbar-default" role="navigation" >
            <div class="container-fluid">
                <div class="navbar-header">
                    <div class="wzl-nav-bar" onclick="history.back(-1)">
                        <span class="glyphicon glyphicon-wzl-back" ></span>余额充积分
                    </div>
                </div>
            </div>
        </nav>
    </div>
    <div class="">
        <div class="pd-line">可用余额(<span class="red" id="has_money">-</span>元)</div>
        <div class="pd-line">
            <span>兑换:</span>
            <input class="in-amount" id="amount" type="number" placeholder="请输入整数金额">
        </div>
        <div class="line-tips">
            <span class="red">提示：</span>
            <span>兑换比例1:10，例 1元=10积分</span>
        </div>
        <div class="submit-btn" onclick="addpoints()">兑换</div>
    </div>



<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="../../js/common.js"></script>
<script src="https://cdn.bootcss.com/layer/3.0.1/layer.min.js"></script>

<script type="application/javascript">
    var has_money;
$(function () {
    initMoney();

})
    //余额充积分
    function addpoints() {
        var reg = /^[0-9]*[1-9][0-9]*$/;
        var amount = $("#amount").val();
        if(!amount){
            layer.msg("请输入要兑换的金额！");
            return;
        }
        if(!reg.test(amount)){
            layer.msg("请输入整数金额");
            return;
        }
        amount = parseFloat(amount);
        has_money = parseFloat(has_money);
        if(amount<=0){
            layer.msg("请输入要兑换的金额！");
            return;
        }
        if(amount>has_money){
            layer.msg("可用余额不足！");
            return;
        }
        layer.confirm('是否兑换?', {icon: 3, title:'提示'}, function(index){
            var phoneNum = window.localStorage.getItem("phoneNum");
            var paramStr = {};
            paramStr.phoneNum =phoneNum;
            paramStr.money = amount;
            var params = {};
            params.paramStr = JSON.stringify(paramStr);
            Common.ajaxWithParamBack("/fenful/api/v1/account/moneyToPoints",params,function (obj) {
                if(obj.code=="200"){
                    layer.msg("兑换成功！");
                    initMoney();
                }else{
                    layer.msg(obj.message);
                }
                layer.close(index);
            });

        });

    }
    function initMoney() {
        var token=localStorage.getItem("token");
        var phoneNum=localStorage.getItem("phoneNum");
        var paramStr = {};
        paramStr.phoneNum =phoneNum;
        var params = {};
        params.paramStr = JSON.stringify(paramStr);
        $.ajax({
            headers: {"phone":phoneNum,"token":token},
            method: "POST",
            data:params,
            url:'/fenful/api/v1/user/getUser',
            datatype: "json",
            async: false,
            success: function(re) {

                var data=JSON.parse(re.data);
                has_money = data.money;
                $("#has_money").text(data.money);
            },
            error: function(re) {

            }
        })

    }
</script>
</body>
</html>